package com.szx.usercenter.controller;

import com.szx.usercenter.domain.SysUser;
import com.szx.usercenter.service.SysUserRoleService;
import com.szx.usercenter.service.SysUserService;
import com.szx.usercenter.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author songzx
 * @create 2024-05-11 10:23
 */
@RestController
@RequestMapping("/sysUser")
@Api(tags = "用户管理")
public class SysUserController {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysUserRoleService sysUserRoleService;

    /**
     * 获取所有用户
     *
     * @return
     */
    @PostMapping("getPageUserList")
    @ApiOperation("获取所有用户")
    public Response getUserList(@RequestBody SysUser sysUser) {
        return sysUserService.getPageUserList(sysUser);
    }

    /**
     * 登录
     *
     * @param sysUser
     * @return
     */
    @ApiOperation("登录")
    @PostMapping("login")
    public Response login(@RequestBody SysUser sysUser, HttpServletRequest request) {
        return sysUserService.login(sysUser, request);
    }

    /**
     * 用户注册
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("register")
    public Response register(String username, String password) {
        return sysUserService.register(username, password);
    }

    /**
     * 根据Token获取当前请求的用户信息
     *
     * @param request
     * @return
     */
    @ApiOperation("获取用户信息")
    @GetMapping("currentUser")
    public Response currentUser(HttpServletRequest request) {
        SysUser sysUser = sysUserService.currentUser(request);
        return Response.rspData(sysUser);
    }

    /**
     * 更新密码
     *
     * @param userName
     * @param newPassword
     * @return
     */
    @ApiOperation("更新密码")
    @PostMapping("updatePassword")
    public Response updatePassword(String userName, String newPassword) {
        return sysUserService.updatePassword(userName, newPassword);
    }

    @ApiOperation("添加新用户")
    @PostMapping("addUser")
    public Response addUser(@RequestBody SysUser sysUser) {
        return sysUserService.addUser(sysUser);
    }

    @ApiOperation("根据用户ID获取用户信息")
    @GetMapping("getUserById")
    public Response getUserById(Long userId) {
        return Response.rspData(sysUserService.getById(userId));
    }

    @ApiOperation("删除用户")
    @DeleteMapping("deleteUser")
    public Response deleteUser(Long userId) {
        sysUserRoleService.removeUserRoles(userId);
        return Response.rspData(sysUserService.removeById(userId));
    }

    @ApiOperation("修改用户信息")
    @PutMapping("updateUser")
    public Response updateUser(@RequestBody SysUser sysUser) {
        return Response.rspData(sysUserService.updateById(sysUser));
    }
}
